<?php
class order_controller extends general_controller
{
    public function action_index()
    {
        if(request('step') == 'search')
        {
            $where = 'WHERE 1';
            $binds = array();
            
            $user_id = (int)request('user_id', 0);
            if(!empty($user_id))
            {
                $where .= ' AND a.user_id = :user_id';
                $binds[':user_id'] = $user_id;
            }
            
            $order_status = request('order_status', '');
            if($order_status != '')
            {
                $where .= ' AND a.order_status = :order_status';
                $binds[':order_status'] = $order_status;
            }
            
            $start_date = request('start_date', '');
            if($start_date != '')
            {
                $where .= ' AND a.created_date >= :start_date';
                $binds[':start_date'] = strtotime($start_date);
            }
            
            $end_date = request('end_date', '');
            if($end_date != '')
            {
                $where .= ' AND a.created_date <= :end_date';
                $binds[':end_date'] = strtotime($end_date);
            }
            
            $order_id = request('order_id', '');
            if($order_id != '')
            {
                $where .= ' AND a.order_id = :order_id';
                $binds[':order_id'] = $order_id;
            }
            
            $order_model = new order_model();
            $total = $order_model->query("SELECT COUNT(*) as count FROM {$order_model->table_name} AS a {$where}", $binds);  
            if($total[0]['count'] > 0)
            {
                $limit = $order_model->set_limit(array(request('page', 1), request('pernum', 10)), $total[0]['count']);
                
                $sql = "SELECT a.id, a.order_id, a.order_status, a.order_amount, a.created_date,
                               b.receiver, b.province, b.city, b.borough, b.address, b.menpai, b.zip, b.mobile
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}order_consignee AS b
                        ON a.order_id = b.order_id
                        {$where}
                        ORDER BY id DESC {$limit}
                       ";
                       
                $list = $order_model->query($sql, $binds);
                $status_map = $order_model->status_map;
                foreach($list as &$v)
                {
                    $v['order_status'] = $status_map[$v['order_status']];
                    $v['created_date'] = date('Y-m-d H:i:s', $v['created_date']);
                }
                
                $results = array
                (
                    'status' => 'success',
                    'list' => $list,
                    'paging' => $order_model->page,
                );
            }
            else
            {
                $results = array('status' => 'nodata');
            }
            
            echo json_encode($results);
        }
        else
        {
            $order_model = new order_model();
            $this->status_map = $order_model->status_map;
            $this->compiler('order/order_list.html');
        }
    }
    /**
     * 导出，excel格式
     */
    public function action_export_excel(){
        $where = ' WHERE 1';
        $status = request('order_status', '');
        if($status !== '')
        {
            $where .= ' AND a.order_status = ' . $status;
        }

        if($_GET['start_date'] != '' && $_GET['end_date'] != ''){
            $where .= ' AND a.created_date>=' . strtotime($_GET['start_date']) .' AND a.created_date<=' . strtotime($_GET['end_date']);
        }

        $order_model = new order_model();
        $sql = "SELECT a.id, a.order_id, a.order_status, a.order_amount, a.created_date,
                               b.receiver, b.province, b.city, b.borough, b.address, b.menpai, b.zip, b.mobile
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}order_consignee AS b
                        ON a.order_id = b.order_id
                        $where
                        ORDER BY id DESC
                       ";
        $list = $order_model->query($sql);
        $status_map = $order_model->status_map;
        foreach($list as &$v)
        {
            $v['order_status'] = $status_map[$v['order_status']];
            $v['created_date'] = date('Y-m-d H:i:s', $v['created_date']);
        }

        /** Include PHPExcel */
        require_once dirname(__FILE__) . '/../../lib/PHPExcel-1.8/Classes/PHPExcel.php';
        $inputFileType = 'Excel2007';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("极好生活")
            ->setLastModifiedBy("极好生活")
            ->setTitle("极好生活")
            ->setSubject("极好生活")
            ->setDescription("极好生活后台系统")
            ->setKeywords("极好生活后台系统")
            ->setCategory("极好生活后台系统");


        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '订单号')
            ->setCellValue('B1', '下单日期')
            ->setCellValue('C1', '总金额')
            ->setCellValue('D1', '收件人姓名')
            ->setCellValue('E1', '收件人电话')
            ->setCellValue('F1', '收件地址')
            ->setCellValue('G1', '订单状态');
            /**->setCellValue('H1', '卡号')
            ->setCellValue('I1', '注册时间')
            ->setCellValue('J1', '账户余额')
            ->setCellValue('K1', '今日收入')
            ->setCellValue('L1', '已提现')**/;
        $i = 2;
        foreach ($list as $v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $i, $v['order_id'])
                ->setCellValue('B' . $i, $v['created_date'])
                ->setCellValue('C' . $i, $v['order_amount'])
                ->setCellValue('D' . $i, $v['receiver'])
                ->setCellValue('E' . $i, $v['mobile'])
                ->setCellValue('F' . $i, $v['province'] . $v['city'] . $v['borough'] . $v['address'] . $v['menpai'])
                ->setCellValue('G' . $i, $v['order_status'])
                /**->setCellValue('H' . $i, $v['bank'])
                ->setCellValue('I' . $i, date('Y-m-d H:i:s', $v['reg_time']))
                ->setCellValue('J' . $i, floatval($v['balance']))
                ->setCellValue('K' . $i, floatval($v['today']))
                ->setCellValue('L' . $i, floatval($v['cashed']))**/;
            $i++;
        }

        $etitle = '极好生活订单列表';
        $objPHPExcel->getActiveSheet()->setTitle($etitle);
        $objPHPExcel->setActiveSheetIndex(0);


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $etitle . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        echo "<script>window.close();</script>";
        exit;
    }

    //金卡vip订单
    public function action_vip_index()
    {
        if(request('step') == 'search')
        {
            $where = "WHERE a.vip='vip1'";
            $binds = array();

            $user_id = (int)request('user_id', 0);
            if(!empty($user_id))
            {
                $where .= ' AND a.user_id = :user_id';
                $binds[':user_id'] = $user_id;
            }

            $order_status = request('order_status', '');
            if($order_status !== '')
            {
                $where .= ' AND a.order_status = :order_status';
                $binds[':order_status'] = $order_status;

                $amount = $this->get_condition_count('order_vip_model', "vip='vip1' AND order_status=" . $order_status, 'SUM(goods_amount)');

            }else{
                $amount = $this->get_condition_count('order_vip_model', "vip='vip1' AND order_status=2", 'SUM(goods_amount)');
            }

            $start_date = request('start_date', '');
            if($start_date != '')
            {
                $where .= ' AND a.created_date >= :start_date';
                $binds[':start_date'] = strtotime($start_date);
            }

            $end_date = request('end_date', '');
            if($end_date != '')
            {
                $where .= ' AND a.created_date <= :end_date';
                $binds[':end_date'] = strtotime($end_date);
            }

            $order_id = request('order_id', '');
            if($order_id != '')
            {
                $where .= ' AND a.order_id = :order_id';
                $binds[':order_id'] = $order_id;
            }

            $order_model = new order_vip_model();
            $total = $order_model->query("SELECT COUNT(*) as count FROM {$order_model->table_name} AS a {$where}", $binds);
            if($total[0]['count'] > 0)
            {
                $limit = $order_model->set_limit(array(request('page', 1), request('pernum', 10)), $total[0]['count']);

                $sql = "SELECT a.id, a.order_id, a.order_status,a.vip, a.goods_amount, a.created_date,
                               b.username
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        {$where}
                        ORDER BY id DESC {$limit}
                       ";

                $list = $order_model->query($sql, $binds);
                $status_map = $order_model->status_map;
                foreach($list as &$v)
                {
                    $v['vip'] = '金卡VIP';
                    $v['order_status'] = $status_map[$v['order_status']];
                    $v['created_date'] = date('Y-m-d H:i:s', $v['created_date']);
                }

                $results = array
                (
                    'status' => 'success',
                    'list' => $list,
                    'paging' => $order_model->page,
                    'amount' => $amount
                );
            }
            else
            {
                $results = array('status' => 'nodata');
            }

            echo json_encode($results);
        }
        else
        {
            $order_model = new order_vip_model();
            $this->status_map = $order_model->status_map;
            $this->amount = $this->get_condition_count('order_vip_model', "vip='vip1' AND order_status = 2", 'SUM(goods_amount)');
            $this->compiler('order/order_vip_list.html');
        }
    }
    /**
     * 导出，excel格式
     */
    public function action_export_excel_vip(){
        $where = "WHERE vip='vip1'";
        $status = request('order_status', '');
        if($status !== '')
        {
            $where .= ' AND a.order_status = ' . $status;
        }

        if($_GET['start_date'] != '' && $_GET['end_date'] != ''){
            $where .= ' AND a.created_date>=' . strtotime($_GET['start_date']) .' AND a.created_date<=' . strtotime($_GET['end_date']);
        }

        $order_model = new order_vip_model();
        $sql = "SELECT a.id, a.order_id, a.order_status,a.vip, a.goods_amount, a.created_date,
                               b.username
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        $where
                        ORDER BY id DESC
                       ";
        $list = $order_model->query($sql);
        $status_map = $order_model->status_map;
        foreach($list as &$v)
        {
            $v['order_status'] = $status_map[$v['order_status']];
            $v['vip'] = '金卡VIP';
            $v['created_date'] = date('Y-m-d H:i:s', $v['created_date']);
        }

        /** Include PHPExcel */
        require_once dirname(__FILE__) . '/../../lib/PHPExcel-1.8/Classes/PHPExcel.php';
        $inputFileType = 'Excel2007';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("极好生活")
            ->setLastModifiedBy("极好生活")
            ->setTitle("极好生活")
            ->setSubject("极好生活")
            ->setDescription("极好生活后台系统")
            ->setKeywords("极好生活后台系统")
            ->setCategory("极好生活后台系统");


        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '订单号')
            ->setCellValue('B1', '下单日期')
            ->setCellValue('C1', '总金额')
            ->setCellValue('D1', '类别')
            ->setCellValue('E1', '用户名')
            ->setCellValue('F1', '订单状态')
            /**->setCellValue('G1', '订单状态');
            ->setCellValue('H1', '卡号')
            ->setCellValue('I1', '注册时间')
            ->setCellValue('J1', '账户余额')
            ->setCellValue('K1', '今日收入')
            ->setCellValue('L1', '已提现')**/;
        $i = 2;
        foreach ($list as $v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $i, $v['order_id'])
                ->setCellValue('B' . $i, $v['created_date'])
                ->setCellValue('C' . $i, $v['goods_amount'])
                ->setCellValue('D' . $i, $v['vip'])
                ->setCellValue('E' . $i, $v['username'])
                ->setCellValue('F' . $i, $v['order_status'])
                /**->setCellValue('G' . $i, $v['order_status'])
                ->setCellValue('H' . $i, $v['bank'])
                ->setCellValue('I' . $i, date('Y-m-d H:i:s', $v['reg_time']))
                ->setCellValue('J' . $i, floatval($v['balance']))
                ->setCellValue('K' . $i, floatval($v['today']))
                ->setCellValue('L' . $i, floatval($v['cashed']))**/;
            $i++;
        }

        $etitle = '极好生活金卡VIP订单列表';
        $objPHPExcel->getActiveSheet()->setTitle($etitle);
        $objPHPExcel->setActiveSheetIndex(0);


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $etitle . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        echo "<script>window.close();</script>";
        exit;
    }
    public function action_vip_zun_index()
    {
        if(request('step') == 'search')
        {
            $where = "WHERE a.vip='vip2'";
            $binds = array();

            $user_id = (int)request('user_id', 0);
            if(!empty($user_id))
            {
                $where .= ' AND a.user_id = :user_id';
                $binds[':user_id'] = $user_id;
            }

            $order_status = request('order_status', '');
            if($order_status !== '')
            {
                $where .= ' AND a.order_status = :order_status';
                $binds[':order_status'] = $order_status;

                $amount = $this->get_condition_count('order_vip_model', "vip='vip2' AND order_status=" . $order_status, 'SUM(goods_amount)');

            }else{
                $amount = $this->get_condition_count('order_vip_model', "vip='vip2' AND order_status=2", 'SUM(goods_amount)');
            }

            $start_date = request('start_date', '');
            if($start_date != '')
            {
                $where .= ' AND a.created_date >= :start_date';
                $binds[':start_date'] = strtotime($start_date);
            }

            $end_date = request('end_date', '');
            if($end_date != '')
            {
                $where .= ' AND a.created_date <= :end_date';
                $binds[':end_date'] = strtotime($end_date);
            }

            $order_id = request('order_id', '');
            if($order_id != '')
            {
                $where .= ' AND a.order_id = :order_id';
                $binds[':order_id'] = $order_id;
            }

            $order_model = new order_vip_model();
            $total = $order_model->query("SELECT COUNT(*) as count FROM {$order_model->table_name} AS a {$where}", $binds);
            if($total[0]['count'] > 0)
            {
                $limit = $order_model->set_limit(array(request('page', 1), request('pernum', 10)), $total[0]['count']);

                $sql = "SELECT a.id, a.order_id, a.order_status,a.vip, a.goods_amount, a.created_date,
                               b.username
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        {$where}
                        ORDER BY id DESC {$limit}
                       ";

                $list = $order_model->query($sql, $binds);
                $status_map = $order_model->status_map;
                foreach($list as &$v)
                {
                    $v['vip'] = '尊贵VIP';
                    $v['order_status'] = $status_map[$v['order_status']];
                    $v['created_date'] = date('Y-m-d H:i:s', $v['created_date']);
                }

                $results = array
                (
                    'status' => 'success',
                    'list' => $list,
                    'paging' => $order_model->page,
                    'amount' => $amount
                );
            }
            else
            {
                $results = array('status' => 'nodata');
            }

            echo json_encode($results);
        }
        else
        {
            $order_model = new order_vip_model();
            $this->status_map = $order_model->status_map;
            $this->amount = $this->get_condition_count('order_vip_model', "vip='vip2' AND order_status = 2", 'SUM(goods_amount)');
            $this->compiler('order/order_vip_zun_list.html');
        }
    }
    /**
     * 导出，excel格式
     */
    public function action_export_excel_zun_vip(){
        $where = "WHERE a.vip='vip2'";
        $status = request('order_status', '');
        if($status !== '')
        {
            $where .= ' AND a.order_status = ' . $status;
        }

        if($_GET['start_date'] != '' && $_GET['end_date'] != ''){
            $where .= ' AND a.created_date>=' . strtotime($_GET['start_date']) .' AND a.created_date<=' . strtotime($_GET['end_date']);
        }

        $order_model = new order_vip_model();
        $sql = "SELECT a.id, a.order_id, a.order_status,a.vip, a.goods_amount, a.created_date,
                               b.username
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        $where
                        ORDER BY id DESC
                       ";
        $list = $order_model->query($sql);
        $status_map = $order_model->status_map;
        foreach($list as &$v)
        {
            $v['order_status'] = $status_map[$v['order_status']];
            $v['vip'] = '尊贵VIP';
            $v['created_date'] = date('Y-m-d H:i:s', $v['created_date']);
        }

        /** Include PHPExcel */
        require_once dirname(__FILE__) . '/../../lib/PHPExcel-1.8/Classes/PHPExcel.php';
        $inputFileType = 'Excel2007';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("极好生活")
            ->setLastModifiedBy("极好生活")
            ->setTitle("极好生活")
            ->setSubject("极好生活")
            ->setDescription("极好生活后台系统")
            ->setKeywords("极好生活后台系统")
            ->setCategory("极好生活后台系统");


        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '订单号')
            ->setCellValue('B1', '下单日期')
            ->setCellValue('C1', '总金额')
            ->setCellValue('D1', '类别')
            ->setCellValue('E1', '用户名')
            ->setCellValue('F1', '订单状态')
            /**->setCellValue('G1', '订单状态');
            ->setCellValue('H1', '卡号')
            ->setCellValue('I1', '注册时间')
            ->setCellValue('J1', '账户余额')
            ->setCellValue('K1', '今日收入')
            ->setCellValue('L1', '已提现')**/;
        $i = 2;
        foreach ($list as $v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $i, $v['order_id'])
                ->setCellValue('B' . $i, $v['created_date'])
                ->setCellValue('C' . $i, $v['goods_amount'])
                ->setCellValue('D' . $i, $v['vip'])
                ->setCellValue('E' . $i, $v['username'])
                ->setCellValue('F' . $i, $v['order_status'])
                /**->setCellValue('G' . $i, $v['order_status'])
                ->setCellValue('H' . $i, $v['bank'])
                ->setCellValue('I' . $i, date('Y-m-d H:i:s', $v['reg_time']))
                ->setCellValue('J' . $i, floatval($v['balance']))
                ->setCellValue('K' . $i, floatval($v['today']))
                ->setCellValue('L' . $i, floatval($v['cashed']))**/;
            $i++;
        }

        $etitle = '极好生活尊贵VIP订单列表';
        $objPHPExcel->getActiveSheet()->setTitle($etitle);
        $objPHPExcel->setActiveSheetIndex(0);


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $etitle . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        echo "<script>window.close();</script>";
        exit;
    }
    //提现列表
    public function action_tixian()
    {
        if(request('step') == 'set'){
            $id = (int)request('id', 0);
            $fial_text =
            $results = [
                'status' => 'fail',
                'msg' => '申请不存在',
            ];
            if(!$id){
                echo json_encode($results, JSON_UNESCAPED_UNICODE);
                exit;
            }
            $cash_model = new user_cash_log_model();
            $info = $cash_model->find(['id' => $id]);
            if(empty($info)){
                echo json_encode($results, JSON_UNESCAPED_UNICODE);
                exit;
            }
            //减去账户金额
            $user_account = new user_account_model();
            $sql = "UPDATE {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user_account SET balance=balance-{$info['money']} WHERE user_id='{$info['user_id']}' AND balance>={$info['money']} LIMIT 1;";
            $ret = $user_account->execute($sql);
            if(!$ret){
                $results = [
                    'status' => 'fail',
                    'msg' => '用户余额不足',
                ];
                echo json_encode($results, JSON_UNESCAPED_UNICODE);
                exit;
            }
            $sql = "UPDATE {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user_cash_log SET `status`=1 WHERE id='{$id}' AND status=2;";
            $ret = $cash_model->execute($sql);
            if(!$ret){
                $results = [
                    'status' => 'fail',
                    'msg' => '网络错误，请联系管理员',
                ];
                echo json_encode($results, JSON_UNESCAPED_UNICODE);
                exit;
            }
            $results = ['status' => 'success'];
            echo json_encode($results, JSON_UNESCAPED_UNICODE);
            exit;
        }
        if(request('step') == 'search')
        {
            $where = 'WHERE 1';
            $binds = array();

            $user_id = (int)request('user_id', 0);
            if(!empty($user_id))
            {
                $where .= ' AND a.user_id = :user_id';
                $binds[':user_id'] = $user_id;
            }

            $status = request('status', '');
            if($status !== '')
            {
                $where .= ' AND a.status = :status';
                $binds[':status'] = $status;
            }

            $start_date = request('start_date', '');
            if($start_date != '')
            {
                $where .= ' AND a.dateline >= :start_date';
                $binds[':start_date'] = strtotime($start_date);
            }

            $end_date = request('end_date', '');
            if($end_date != '')
            {
                $where .= ' AND a.dateline <= :end_date';
                $binds[':end_date'] = strtotime($end_date);
            }


            $user_cash_log = new user_cash_log_model();
            $total = $user_cash_log->query("SELECT COUNT(*) as count FROM {$user_cash_log->table_name} AS a {$where}", $binds);
            if($total[0]['count'] > 0)
            {
                $limit = $user_cash_log->set_limit(array(request('page', 1), request('pernum', 10)), $total[0]['count']);

                $sql = "SELECT a.*,
                               b.username
                        FROM {$user_cash_log->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        {$where}
                        ORDER BY id DESC {$limit}
                       ";

                $list = $user_cash_log->query($sql, $binds);
                $status_texy = ['失败','成功','申请中'];
                foreach($list as &$v)
                {
                    $v['dateline'] = date('Y-m-d H:i:s', $v['dateline']);
                    $v['status_text'] = $status_texy[$v['status']];
                    $v['money_tax'] = $v['money'] - $v['money'] * 0.05;
                }

                $results = array
                (
                    'status' => 'success',
                    'list' => $list,
                    'paging' => $user_cash_log->page,
                );
            }
            else
            {
                $results = array('status' => 'nodata');
            }

            echo json_encode($results, JSON_UNESCAPED_UNICODE);
        }
        else
        {
            //$order_model = new user_cash_log_model();
            $this->compiler('order/user_cash_list.html');
        }
    }
    /**
     * 导出，excel格式
     */
    public function action_export_excel_tixian(){
        $where = ' WHERE 1';
        $status = request('order_status', '');
        if($status !== '')
        {
            $where .= ' AND a.status = ' . $status;
        }

        if($_GET['start_date'] != '' && $_GET['end_date'] != ''){
            $where .= ' AND a.dateline>=' . strtotime($_GET['start_date']) .' AND a.dateline<=' . strtotime($_GET['end_date']);
        }

        $user_cash_log = new user_cash_log_model();
        $sql = "SELECT a.*,
                               b.username
                        FROM {$user_cash_log->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        $where
                        ORDER BY id DESC
                       ";
        $list = $user_cash_log->query($sql);
        foreach($list as &$v)
        {
            $v['dateline'] = date('Y-m-d H:i:s', $v['dateline']);
            $v['status'] = ($v['status'] == 0) ? '失败' : '成功';
        }

        /** Include PHPExcel */
        require_once dirname(__FILE__) . '/../../lib/PHPExcel-1.8/Classes/PHPExcel.php';
        $inputFileType = 'Excel2007';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("极好生活")
            ->setLastModifiedBy("极好生活")
            ->setTitle("极好生活")
            ->setSubject("极好生活")
            ->setDescription("极好生活后台系统")
            ->setKeywords("极好生活后台系统")
            ->setCategory("极好生活后台系统");


        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '用户')
            ->setCellValue('B1', '提现金额')
            ->setCellValue('C1', '提现状态')
            ->setCellValue('D1', '提现时间')
            /**->setCellValue('E1', '用户名')
            ->setCellValue('F1', '订单状态')
            ->setCellValue('G1', '订单状态');
            ->setCellValue('H1', '卡号')
            ->setCellValue('I1', '注册时间')
            ->setCellValue('J1', '账户余额')
            ->setCellValue('K1', '今日收入')
            ->setCellValue('L1', '已提现')**/;
        $i = 2;
        foreach ($list as $v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $i, $v['username'])
                ->setCellValue('B' . $i, $v['money'])
                ->setCellValue('C' . $i, $v['status'])
                ->setCellValue('D' . $i, $v['dateline'])
                /**->setCellValue('E' . $i, $v['username'])
                ->setCellValue('F' . $i, $v['order_status'])
                ->setCellValue('G' . $i, $v['order_status'])
                ->setCellValue('H' . $i, $v['bank'])
                ->setCellValue('I' . $i, date('Y-m-d H:i:s', $v['reg_time']))
                ->setCellValue('J' . $i, floatval($v['balance']))
                ->setCellValue('K' . $i, floatval($v['today']))
                ->setCellValue('L' . $i, floatval($v['cashed']))**/;
            $i++;
        }

        $etitle = '极好生活提现列表';
        $objPHPExcel->getActiveSheet()->setTitle($etitle);
        $objPHPExcel->setActiveSheetIndex(0);


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $etitle . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        echo "<script>window.close();</script>";
        exit;
    }
    //分享返佣
    public function action_share_list()
    {
        if(request('step') == 'search')
        {
            $where = 'WHERE 1';
            $count_where = null;
            $binds = array();

            $user_id = (int)request('user_id', 0);
            if(!empty($user_id))
            {
                $where .= ' AND a.user_id = :user_id';
                $binds[':user_id'] = $user_id;
            }

            $order_status = request('order_status', '');
            if($order_status !== '')
            {
                $where .= ' AND a.order_status = :order_status';
                $binds[':order_status'] = $order_status;
            }

            $start_date = request('start_date', '');
            if($start_date != '')
            {
                $where .= ' AND a.dateline >= :start_date';
                $s = strtotime($start_date);
                $count_where = 'dateline>=' . $s;
                $binds[':start_date'] = $s;
            }

            $end_date = request('end_date', '');
            if($end_date != '')
            {
                $where .= ' AND a.dateline <= :end_date';
                $e = strtotime($end_date);
                $count_where .= ' AND dateline<=' . $e;
                $binds[':end_date'] = strtotime($end_date);
            }

            $order_id = request('order_id', '');
            if($order_id != '')
            {
                $where .= ' AND a.order_id = :order_id';
                $binds[':order_id'] = $order_id;
            }

            $order_model = new user_share_receive_model();
            $amount = $this->get_condition_count('user_share_receive_model', $count_where, 'SUM(money)');
            $total = $order_model->query("SELECT COUNT(*) as count FROM {$order_model->table_name} AS a {$where}", $binds);
            if($total[0]['count'] > 0)
            {
                $limit = $order_model->set_limit(array(request('page', 1), request('pernum', 10)), $total[0]['count']);

                $sql = "SELECT a.*,
                               b.username,
                               c.goods_name
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        inner join {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}goods as c
                        on a.goods_id=c.goods_id
                        {$where}
                        ORDER BY id DESC {$limit}
                       ";

                $list = $order_model->query($sql, $binds);
                foreach($list as &$v)
                {
                    $v['dateline'] = date('Y-m-d H:i:s', $v['dateline']);
                }

                $results = array
                (
                    'status' => 'success',
                    'list' => $list,
                    'paging' => $order_model->page,
                    'amount' => floatval($amount)
                );
            }
            else
            {
                $results = array('status' => 'nodata','amount' => 0);
            }

            echo json_encode($results);
        }
        else
        {
            $order_model = new order_vip_model();
            $this->status_map = $order_model->status_map;
            $this->amount = $this->get_condition_count('user_share_receive_model', null, 'SUM(money)');
            $this->compiler('order/user_share_receive_list.html');
        }
    }
    /**
     * 导出，excel格式
     */
    public function action_export_excel_share(){
        $where = ' WHERE 1';
        if($_GET['start_date'] != '' && $_GET['end_date'] != ''){
            $where = ' WHERE a.dateline>=' . strtotime($_GET['start_date']) .' AND a.dateline<=' . strtotime($_GET['end_date']);
        }
        $user_share_receive_model = new user_share_receive_model();
        $sql = "SELECT a.*,
                               b.username,
                               c.goods_name
                        FROM {$user_share_receive_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        inner join {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}goods as c
                        on a.goods_id=c.goods_id
                        $where
                        ORDER BY id DESC
                       ";
        $list = $user_share_receive_model->query($sql);
        foreach($list as &$v)
        {
            $v['dateline'] = date('Y-m-d H:i:s', $v['dateline']);
        }

        /** Include PHPExcel */
        require_once dirname(__FILE__) . '/../../lib/PHPExcel-1.8/Classes/PHPExcel.php';
        $inputFileType = 'Excel2007';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("极好生活")
            ->setLastModifiedBy("极好生活")
            ->setTitle("极好生活")
            ->setSubject("极好生活")
            ->setDescription("极好生活后台系统")
            ->setKeywords("极好生活后台系统")
            ->setCategory("极好生活后台系统");


        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '用户')
            ->setCellValue('B1', '分享商品')
            ->setCellValue('C1', '佣金')
            ->setCellValue('D1', '获得时间')
            /**->setCellValue('E1', '用户名')
            ->setCellValue('F1', '订单状态')
            ->setCellValue('G1', '订单状态');
            ->setCellValue('H1', '卡号')
            ->setCellValue('I1', '注册时间')
            ->setCellValue('J1', '账户余额')
            ->setCellValue('K1', '今日收入')
            ->setCellValue('L1', '已提现')**/;
        $i = 2;
        foreach ($list as $v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $i, $v['username'])
                ->setCellValue('B' . $i, $v['goods_name'])
                ->setCellValue('C' . $i, $v['money'])
                ->setCellValue('D' . $i, $v['dateline'])
                /**->setCellValue('E' . $i, $v['username'])
                ->setCellValue('F' . $i, $v['order_status'])
                ->setCellValue('G' . $i, $v['order_status'])
                ->setCellValue('H' . $i, $v['bank'])
                ->setCellValue('I' . $i, date('Y-m-d H:i:s', $v['reg_time']))
                ->setCellValue('J' . $i, floatval($v['balance']))
                ->setCellValue('K' . $i, floatval($v['today']))
                ->setCellValue('L' . $i, floatval($v['cashed']))**/;
            $i++;
        }

        $etitle = '极好生活分享返佣列表';
        $objPHPExcel->getActiveSheet()->setTitle($etitle);
        $objPHPExcel->setActiveSheetIndex(0);


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $etitle . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        echo "<script>window.close();</script>";
        exit;
    }
    //金卡返佣
    public function action_king_rake()
    {
        if(request('step') == 'search')
        {
            $where = $count_where = "WHERE a.prize<='2'";
            $count_where = ' prize<=2';
            $binds = array();

            $user_id = (int)request('user_id', 0);
            if(!empty($user_id))
            {
                $where .= ' AND a.user_id = :user_id';
                $count_where .= ' AND user_id =' . $user_id;
                $binds[':user_id'] = $user_id;
            }

            $prize = request('prize', '');
            if($prize !== '')
            {
                $where .= ' AND a.prize = :prize';
                $count_where = ' prize =' . $prize;
                $binds[':prize'] = $prize;
            }

            $start_date = request('start_date', '');
            if($start_date != '')
            {
                $where .= ' AND a.dateline >= :start_date';
                $s = strtotime($start_date);
                $count_where .= ' AND dateline >= ' . $s;
                $binds[':start_date'] = $s;
            }

            $end_date = request('end_date', '');
            if($end_date != '')
            {
                $where .= ' AND a.dateline <= :end_date';
                $e = strtotime($end_date);
                $count_where .= ' AND dateline <= ' . $e;
                $binds[':end_date'] = $e;
            }

            $order_id = request('order_id', '');
            if($order_id != '')
            {
                $where .= ' AND a.order_id = :order_id';
                $binds[':order_id'] = $order_id;
            }

            $order_model = new user_account_log_model();
            $amount = $this->get_condition_count('user_account_log_model', $count_where, 'SUM(balance)');
            $total = $order_model->query("SELECT COUNT(*) as count FROM {$order_model->table_name} AS a {$where}", $binds);
            if($total[0]['count'] > 0)
            {
                $limit = $order_model->set_limit(array(request('page', 1), request('pernum', 10)), $total[0]['count']);

                $sql = "SELECT a.*,
                               b.username
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        {$where}
                        ORDER BY id DESC {$limit}
                       ";

                $list = $order_model->query($sql, $binds);
                //0金卡VIP直推奖1金卡VIP分享奖2金卡VIP管理奖3尊贵VIP直推奖4尊贵VIP直推奖5尊贵VIP管理奖6市场总监金卡收入7市场总监尊贵8同级市场总监收入金卡9同级市场总监收入尊贵
                $prize_text = ['金卡VIP补贴', '金卡VIP收藏', '金卡VIP管理补贴'];
                $type_text = ['直推补贴','收藏','服务补贴','分享产品返利'];
                foreach($list as &$v)
                {
                    $v['prize'] = $prize_text[$v['prize']];
                    $v['type'] = $type_text[$v['type']];
                    $v['dateline'] = date('Y-m-d H:i:s', $v['dateline']);
                }

                $results = array
                (
                    'status' => 'success',
                    'list' => $list,
                    'paging' => $order_model->page,
                    'amount' => $amount
                );
            }
            else
            {
                $results = array('status' => 'nodata', 'amount' => 0);
            }

            echo json_encode($results);
        }
        else
        {
            $this->amount = $this->get_condition_count('user_account_log_model', 'prize<=2', 'SUM(balance)');
            $this->compiler('order/king_rake_list.html');
        }
    }
    /**
     * 导出，excel格式
     */
    public function action_export_excel_king_rake(){
        $where = ' WHERE a.prize<=2';
        $prize = request('prize', '');
        if($prize !== '')
        {
            $where = ' WHERE a.prize =' . $prize;
        }
        if($_GET['start_date'] != '' && $_GET['end_date'] != ''){
            $where .= ' AND a.dateline>=' . strtotime($_GET['start_date']) .' AND a.dateline<=' . strtotime($_GET['end_date']);
        }

        $order_model = new user_account_log_model();
        $sql = "SELECT a.*,
                               b.username
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        $where
                        ORDER BY id DESC
                       ";

        $list = $order_model->query($sql);
        $prize_text = ['金卡VIP直推补贴', '金卡VIP收藏', '金卡VIP服务补贴'];
        $type_text = ['直推补贴','收藏','服务补贴','分享产品返利'];
        foreach($list as &$v)
        {
            $v['prize'] = $prize_text[$v['prize']];
            $v['type'] = $type_text[$v['type']];
            $v['dateline'] = date('Y-m-d H:i:s', $v['dateline']);
        }

        /** Include PHPExcel */
        require_once dirname(__FILE__) . '/../../lib/PHPExcel-1.8/Classes/PHPExcel.php';
        $inputFileType = 'Excel2007';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("极好生活")
            ->setLastModifiedBy("极好生活")
            ->setTitle("极好生活")
            ->setSubject("极好生活")
            ->setDescription("极好生活后台系统")
            ->setKeywords("极好生活后台系统")
            ->setCategory("极好生活后台系统");


        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '用户')
            ->setCellValue('B1', '补贴')
            ->setCellValue('C1', '补贴金额')
            ->setCellValue('D1', '类别')
            ->setCellValue('E1', '获得时间')
            /**->setCellValue('F1', '订单状态')
            ->setCellValue('G1', '订单状态');
            ->setCellValue('H1', '卡号')
            ->setCellValue('I1', '注册时间')
            ->setCellValue('J1', '账户余额')
            ->setCellValue('K1', '今日收入')
            ->setCellValue('L1', '已提现')**/;
        $i = 2;
        foreach ($list as $v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $i, $v['username'])
                ->setCellValue('B' . $i, $v['prize'])
                ->setCellValue('C' . $i, $v['balance'])
                ->setCellValue('D' . $i, $v['type'])
                ->setCellValue('E' . $i, $v['dateline'])
                /**->setCellValue('F' . $i, $v['order_status'])
                ->setCellValue('G' . $i, $v['order_status'])
                ->setCellValue('H' . $i, $v['bank'])
                ->setCellValue('I' . $i, date('Y-m-d H:i:s', $v['reg_time']))
                ->setCellValue('J' . $i, floatval($v['balance']))
                ->setCellValue('K' . $i, floatval($v['today']))
                ->setCellValue('L' . $i, floatval($v['cashed']))**/;
            $i++;
        }

        $etitle = '极好生活金卡VIP返佣列表';
        $objPHPExcel->getActiveSheet()->setTitle($etitle);
        $objPHPExcel->setActiveSheetIndex(0);


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $etitle . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        echo "<script>window.close();</script>";
        exit;
    }
    //尊贵返佣
    public function action_zun_rake()
    {
        if(request('step') == 'search')
        {
            $where = "WHERE a.prize>='3' AND a.prize<=5";
            $count_where = ' prize>=3 AND prize<=5';
            $binds = array();

            $user_id = (int)request('user_id', 0);
            if(!empty($user_id))
            {
                $where .= ' AND a.user_id = :user_id';
                $count_where .= ' AND user_id' . $user_id;
                $binds[':user_id'] = $user_id;
            }

            $prize = request('prize', '');
            if($prize !== '')
            {
                $where .= ' AND a.prize = :prize';
                $count_where = 'prize=' . $prize;
                $binds[':prize'] = $prize;
            }

            $start_date = request('start_date', '');
            if($start_date != '')
            {
                $where .= ' AND a.dateline >= :start_date';
                $s = strtotime($start_date);
                $count_where .= ' AND dateline>=' . $s;
                $binds[':start_date'] = $s;
            }

            $end_date = request('end_date', '');
            if($end_date != '')
            {
                $where .= ' AND a.dateline <= :end_date';
                $e = strtotime($end_date);
                $count_where .= ' AND dateline<=' . $e;
                $binds[':end_date'] = $e;
            }

            $order_id = request('order_id', '');
            if($order_id != '')
            {
                $where .= ' AND a.order_id = :order_id';
                $binds[':order_id'] = $order_id;
            }

            $order_model = new user_account_log_model();
            $amount = $this->get_condition_count('user_account_log_model', $count_where, 'SUM(balance)');
            $total = $order_model->query("SELECT COUNT(*) as count FROM {$order_model->table_name} AS a {$where}", $binds);
            if($total[0]['count'] > 0)
            {
                $limit = $order_model->set_limit(array(request('page', 1), request('pernum', 10)), $total[0]['count']);

                $sql = "SELECT a.*,
                               b.username
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        {$where}
                        ORDER BY id DESC {$limit}
                       ";

                $list = $order_model->query($sql, $binds);
                //3尊贵VIP直推奖4尊贵VIP直推奖5尊贵VIP管理奖6市场总监金卡收入7市场总监尊贵8同级市场总监收入金卡9同级市场总监收入尊贵
                $prize_text = [3 => '尊贵VIP直推补贴', 4 => '尊贵VIP收藏', 5 => '尊贵VIP服务补贴'];
                $type_text = ['直推补贴','分享补贴','服务补贴','分享产品返利'];
                foreach($list as &$v)
                {
                    $v['prize'] = $prize_text[$v['prize']];
                    $v['type'] = $type_text[$v['type']];
                    $v['dateline'] = date('Y-m-d H:i:s', $v['dateline']);
                }

                $results = array
                (
                    'status' => 'success',
                    'list' => $list,
                    'paging' => $order_model->page,
                    'amount' => $amount
                );
            }
            else
            {
                $results = array('status' => 'nodata','amount' => 0);
            }

            echo json_encode($results);
        }
        else
        {
            $order_model = new order_vip_model();
            $this->status_map = $order_model->status_map;
            $this->amount = $this->get_condition_count('user_account_log_model', 'prize>=3 and prize<=5', 'SUM(balance)');
            $this->compiler('order/zun_rake_list.html');
        }
    }
    /**
     * 导出，excel格式
     */
    public function action_export_excel_zun_rake(){
        $where = " WHERE a.prize>='3' AND a.prize<='5'";
        $prize = request('prize', '');
        if($prize !== '')
        {
            $where = ' WHERE a.prize =' . $prize;
        }
        if($_GET['start_date'] != '' && $_GET['end_date'] != ''){
            $where .= ' AND a.dateline>=' . strtotime($_GET['start_date']) .' AND a.dateline<=' . strtotime($_GET['end_date']);
        }

        $order_model = new user_account_log_model();
        $sql = "SELECT a.*,
                               b.username
                        FROM {$order_model->table_name} AS a
                        INNER JOIN {$GLOBALS['mysql']['MYSQL_DB_TABLE_PRE']}user AS b
                        ON a.user_id = b.user_id
                        $where
                        ORDER BY id DESC
                       ";

        $list = $order_model->query($sql);
        $prize_text = [3 => '尊贵VIP直推补贴', 4 => '尊贵VIP收藏', 5 => '尊贵VIP管理补贴'];
        $type_text = ['直推补贴','收藏','服务补贴','分享产品返利'];
        foreach($list as &$v)
        {
            $v['prize'] = $prize_text[$v['prize']];
            $v['type'] = $type_text[$v['type']];
            $v['dateline'] = date('Y-m-d H:i:s', $v['dateline']);
        }

        /** Include PHPExcel */
        require_once dirname(__FILE__) . '/../../lib/PHPExcel-1.8/Classes/PHPExcel.php';
        $inputFileType = 'Excel2007';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("极好生活")
            ->setLastModifiedBy("极好生活")
            ->setTitle("极好生活")
            ->setSubject("极好生活")
            ->setDescription("极好生活后台系统")
            ->setKeywords("极好生活后台系统")
            ->setCategory("极好生活后台系统");


        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '用户')
            ->setCellValue('B1', '补贴')
            ->setCellValue('C1', '补贴金额')
            ->setCellValue('D1', '类别')
            ->setCellValue('E1', '获得时间')
            /**->setCellValue('F1', '订单状态')
            ->setCellValue('G1', '订单状态');
            ->setCellValue('H1', '卡号')
            ->setCellValue('I1', '注册时间')
            ->setCellValue('J1', '账户余额')
            ->setCellValue('K1', '今日收入')
            ->setCellValue('L1', '已提现')**/;
        $i = 2;
        foreach ($list as $v){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $i, $v['username'])
                ->setCellValue('B' . $i, $v['prize'])
                ->setCellValue('C' . $i, $v['balance'])
                ->setCellValue('D' . $i, $v['type'])
                ->setCellValue('E' . $i, $v['dateline'])
                /**->setCellValue('F' . $i, $v['order_status'])
                ->setCellValue('G' . $i, $v['order_status'])
                ->setCellValue('H' . $i, $v['bank'])
                ->setCellValue('I' . $i, date('Y-m-d H:i:s', $v['reg_time']))
                ->setCellValue('J' . $i, floatval($v['balance']))
                ->setCellValue('K' . $i, floatval($v['today']))
                ->setCellValue('L' . $i, floatval($v['cashed']))**/;
            $i++;
        }

        $etitle = '极好生活尊贵VIP返佣列表';
        $objPHPExcel->getActiveSheet()->setTitle($etitle);
        $objPHPExcel->setActiveSheetIndex(0);


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $etitle . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        echo "<script>window.close();</script>";
        exit;
    }
    /**
 * 获取符合条件的相关数据总数
 */
    private function get_condition_count($model_name, $condition = null, $col = null)
    {
        $condition = empty($condition) ? '1' : $condition;
        $col = empty($col) ? 'COUNT(*)' : $col;
        $model = new $model_name();
        $sql = "SELECT {$col} AS count FROM {$model->table_name} WHERE {$condition}";
        $rs = $model->query($sql);
        return $rs[0]['count'];
    }

    public function action_view()
    {
        $condition = array('order_id' => request('id', ''));
        $order_model = new order_model();
        if($order = $order_model->find($condition))
        {
            $vcache = vcache::instance();
            //$payment_map = $vcache->payment_method_model('indexed_list');
            $shipping_map = $vcache->shipping_method_model('indexed_list');
            $order['payment_method_name'] = '微信支付';
            $order['shipping_method_name'] = '线下配送';



            $setting_model = new setting_model();
            $rs = $setting_model->get_config();
            $this->business = json_decode($rs['business'], true);
            if(!in_array($this->business['business'], array(1,2))){
                $this->prompt('error', '请先设置经营范围');
            }

            $consignee_model = new order_consignee_model();
            $this->consignee = $consignee_model->find($condition);
            $this->order = $order;
            $this->status_map = $order_model->status_map;
            //用户信息
            $user_model = new user_model();
            $this->user = $user_model->find(array('user_id' => $order['user_id']));
            //商品列表
            $order_goods_model = new order_goods_model();
            $this->goods_list = $order_goods_model->get_goods_list($condition['order_id']);
            //发货列表
            if($this->business['business'] == 1){
                $this->carrier_list = $vcache->shipping_self_model('indexed_list');
            }else{
                $this->carrier_list = $vcache->shipping_carrier_model('indexed_list');
            }

            $shipping_model = new order_shipping_model();
            $shipped_list = $shipping_model->find_all($condition, 'dateline DESC');
            if(!empty($shipped_list))
            {
                foreach($shipped_list as &$v) $v['carrier'] = $this->carrier_list[$v['carrier_id']]['name'];
            }
            $this->shipped_list = $shipped_list;
            //日志列表
            $log_model = new order_log_model();
            if($this->log_list = $log_model->find_all($condition, 'dateline DESC'))
            {
                $this->admin_list = $vcache->admin_model('indexed_list');
                $this->operate_map = $log_model->operate_map;
            }
            
            $this->compiler('order/order.html');
        }
        else
        {
            $this->prompt('error', '无法找到相应的订单记录');
        }
    }
    
    public function action_operate()
    {
        $order_id = request('id', '');
        $condition = array('order_id' => $order_id);
        $order_model = new order_model();
        if($order = $order_model->find($condition))
        {
            $errno = 0;
            switch(request('step'))
            {
                case 'consignee':
                    
                    if($order['order_status'] == 1 || $order['order_status'] == 2)
                    {
                        $data = array
                        (
                            'receiver' => trim(request('receiver', '')),
                            'province' => trim(request('province', '')),
                            'city' => trim(request('city', '')),
                            'borough' => trim(request('borough', '')),
                            'address' => trim(request('address', '')),
                            'menpai' => trim(request('menpai', '')),
                            'zip' => trim(request('zip', '')),
                            'mobile' => trim(request('mobile', '')),
                        );
                        $consignee_model = new order_consignee_model();
                        $verifier = $consignee_model->verifier($data);
                        if(TRUE === $verifier)
                        {
                            if($consignee_model->update(array('order_id' => $order_id), $data) > 0)
                            {
                                $cause = trim(request('cause', ''));
                                $log_model = new order_log_model();
                                $log_model->record($order_id, 'consignee', $cause);
                            }
                            else
                            {
                                $errno = 1;
                            }
                        }
                        else
                        {
                             $this->prompt('error', $verifier);
                        }
                    }
                    else
                    {
                        $errno = 2;
                    }
                
                break;
                
                case 'amount': //更改金额
                    
                    if($order['order_status'] == 1)
                    {
                        $order_amount = sprintf('%.2f', abs(request('order_amount', 0, 'post')));
                        if($order_model->update($condition, array('order_amount' => $order_amount)) > 0)
                        {
                            $cause = request('cause', '', 'post');
                            $log_model = new order_log_model();
                            $log_model->record($order_id, 'amount', $cause);
                        }
                        else
                        {
                            $errno = 1;
                        }
                    }
                    else
                    {
                        $errno = 2;
                    }
                
                break;
                
                case 'cancel': //取消交易
                    
                    if($order['order_status'] == 1)
                    {
                        if($order_model->update($condition, array('order_status' => 0)) > 0)
                        {
                            $order_goods_model = new order_goods_model();
                            $order_goods_model->restocking($order_id);
                            $cause = trim(request('cause', ''));
                            $log_model = new order_log_model();
                            $log_model->record($order_id, 'cancel', $cause);
                        }
                        else
                        {
                            $errno = 1;
                        }
                    }
                    else
                    {
                        $errno = 2;
                    }
                    
                break;
                
                case 'resume': //恢复被取消交易
                
                    if($order['order_status'] == 0)
                    {
                        if($order_model->update($condition, array('order_status' => 1)) > 0)
                        {
                            $order_goods_model = new order_goods_model();
                            $order_goods_model->restocking($order_id, 'decr');
                            $cause = trim(request('cause', ''));
                            $log_model = new order_log_model();
                            $log_model->record($order_id, 'resume', $cause);
                        }
                        else
                        {
                            $errno = 1;
                        }
                    }
                    else
                    {
                        $errno = 2;
                    }
                
                break;
                
                case 'shipping':
                    
                    if($order['order_status'] >= 2 || ($order['order_status'] != 0 && $order['payment_method'] == 2))
                    {
                        $data = array
                        (
                            'order_id' => $order_id,
                            'carrier_id' => (int)request('carrier_id', ''),
                            'tracking_no' => request('tracking_no', ''),
                            'memos' => trim(request('memos', '')),
                            'dateline' => $_SERVER['REQUEST_TIME'],
                        );
                        $shipping_model = new order_shipping_model();
                        if($shipping_model->create($data) > 0)
                        {
                            $order_model->update(array('order_id' => $order_id), array('order_status' => 3));
                        }
                        else
                        {
                            $errno = 1;
                        }
                    }
                    else
                    {
                        $errno = 2;
                    }
                    
                break;
            }
            
            $errormap = array
            (
                0 => '操作成功',
                1 => '操作失败',
                2 => '当前订单无法进行此操作',
            );
            $this->prompt($errno == 0 ? 'success' : 'error', $errormap[$errno], url($this->MOD.'/order', 'view', array('id' => $order_id)));
        }
        else
        {
            $this->prompt('error', '订单不存在');
        }
    }
    
    public function action_delete()
    {
        $id = request('id');
        $condition = array('order_id' => $id);
        $order_model = new order_model();
        if($order = $order_model->find($condition))
        {
            if($order['order_status'] == 0)
            {
                if($order_model->delete($condition) > 0)
                {
                    //删除订单商品
                    $order_goods_model = new order_goods_model();
                    $goods_list = $order_goods_model->find_all($condition);
                    $order_goods_model->delete($condition);
                    if($goods_list)
                    {
                        $order_goods_optional = new order_goods_optional_model();
                        foreach($goods_list as $v)
                        {
                            $order_goods_optional->delete(array('map_id' => $v['id']));
                        }
                    }
                    
                    //删除订单收件人地址
                    $order_consignee_model = new order_consignee_model();
                    $order_consignee_model->delete($condition);
                    $this->prompt('success', '删除成功', url($this->MOD.'/order', 'index'));
                }
                else
                {
                    $this->prompt('error', '删除失败');
                } 
            }
            else
            {
                $this->prompt('error', '该订单无法删除');
            }
        }
        else
        {
            $this->prompt('error', '订单不存在');
        }  
    }
    
}